﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InfoPick.Lib.Entity;
using System.Xml.Linq;

namespace InfoPick.Lib.Service
{
    public class ServerService
    {
        private string containerName;

        public ServerService(string containerName)
        {
            this.containerName = containerName;
        }
        public List<Server> LoadList()
        {
            List<Server> list = new List<Server>();
            XDocument xdoc = XDocument.Load(Global.XmlDataPath);

            var ele = xdoc.Root.Element(containerName);
            foreach (var mgw in ele.Elements())
            {
                Server obj = new Server();
                obj.FromXml(mgw.ToString());
                list.Add(obj);
            }

            return list;
        }
        public List<Server> LoadList(ref string defaultSer)
        {
            List<Server> list = new List<Server>();
            XDocument xdoc = XDocument.Load(Global.XmlDataPath);

            var ele = xdoc.Root.Element(containerName);
            var attr = ele.Attribute("Default");
            if (attr != null)
                defaultSer = attr.Value;

            foreach (var mgw in ele.Elements())
            {
                Server obj = new Server();
                obj.FromXml(mgw.ToString());
                list.Add(obj);
            }

            return list;
        }

        public void SaveOrUpdate(Server obj)
        {
            XDocument xdoc = XDocument.Load(Global.XmlDataPath);
            var exist = xdoc.Root.Element(containerName).Elements().
                SingleOrDefault(c => c.Attribute("Name").Value.ToLower() == obj.Name.ToLower() &&
                c.Attribute("Guid").Value != obj.Guid.ToString());
            if (exist != null)
            {
                throw new Exception(string.Format("已存在别名为‘{0}’的服务器！", obj.Name));
            }

            if (obj.Guid == default(Guid))
            {
                obj.Guid = Guid.NewGuid();
                xdoc.Root.Element(containerName).Add(XElement.Parse(obj.ToXml()));
            }
            else
            {
                XElement old = xdoc.Root.Element(containerName).Elements().SingleOrDefault(c => c.Attribute("Guid").Value == obj.Guid.ToString());
                old.SetAttributeValue("Name", obj.Name);
                old.SetAttributeValue("Address", obj.Address);
                old.SetAttributeValue("Port", obj.Port);
                old.SetAttributeValue("UserName", obj.UserName);
                old.SetAttributeValue("Password", Convert.ToBase64String(Encoding.Default.GetBytes(obj.Password)));
            }

            xdoc.Save(Global.XmlDataPath);
        }

        public void Delete(Guid guid)
        {
            XDocument xdoc = XDocument.Load(Global.XmlDataPath);

            var ele = xdoc.Root.Element(containerName).Elements().
                SingleOrDefault(c => c.Attribute("Guid").Value == guid.ToString());
            if (ele != null)
            {
                ele.Remove();
                xdoc.Save(Global.XmlDataPath);
            }
        }

        public void SetDefault(Server server)
        {
            XDocument xdoc = XDocument.Load(Global.XmlDataPath);

            var ele = xdoc.Root.Element(containerName);
            ele.SetAttributeValue("Default", server.Guid);
            xdoc.Save(Global.XmlDataPath);
        }
    }
}
